<?php
include ($_SERVER['DOCUMENT_ROOT']."/CTAN/config/ConnectDB.php");
class NhomNhac extends ConnectDB{
    private $maNhom, $tenNhom, $ngayThanhLap, $thongTin, $luotLike;
    private $tableName="nhom_nhac";
    public function getMaNhom() {
        return $this->maNhom;
    }

    public function setMaNhom($maNhom) {
        $this->maNhom = $maNhom;
    }

    public function getTenNhom() {
        return $this->tenNhom;
    }

    public function setTenNhom($tenNhom) {
        $this->tenNhom = $tenNhom;
    }

    public function getNgayThanhLap() {
        return $this->ngayThanhLap;
    }

    public function setNgayThanhLap($ngayThanhLap) {
        $this->ngayThanhLap = $ngayThanhLap;
    }

    public function getThongTin() {
        return $this->thongTin;
    }

    public function setThongTin($thongTin) {
        $this->thongTin = $thongTin;
    }

    public function getLuotLike() {
        return $this->luotLike;
    }

    public function setLuotLike($luotLike) {
        $this->luotLike = $luotLike;
    }

    public function getTableName() {
        return $this->tableName;
    }

    public function setTableName($tableName) {
        $this->tableName = $tableName;
    }

    public function themNhomNhac(){
        $insert_query = "INSERT INTO " .$this->tableName. " (ten_nhom, ngay_thanh_lap, thong_tin, luot_like) VALUES (
                        '".$this->tenNhom."',
                        '".$this->ngayThanhLap."',
                        '".$this->thongTin."',
                        '".$this->luotLike."')";
        $this->setQuery($insert_query);
        //$result = mysql_query($insert_query, $connection) or die("SQL Error 1: " . mysql_error());
        if ($this->executeQuery()){
            $getIdCommand = "SELECT MAX(ma_nhom) as ma_nhom FROM ".$this->tableName;
            $this->setQuery($getIdCommand);
            $result = $this->executeQuery();
            while ($row = mysql_fetch_array($result)) {
                $this->id=$row["ma_nhom"];
            }
            //tra ve bien id de luu hinh
            return $this->id;
        }else return $result;
    }
    public function capNhatNhomNhac(){
	$update_query = "UPDATE " .$this->tableName. " SET ten_nhom='".$this->tenNhom."',
                        ngay_thanh_lap='".$this->ngayThanhLap."',
                        thong_tin='".$this->thongTin."' WHERE ma_nhom='".$this->maNhom."'";
        $this->setQuery($update_query);
	return $this->executeQuery();
    }
    public function xoaNhomNhac(){
	$delete_query = "DELETE FROM " .$this->tableName. " WHERE ma_nhom='".$this->maNhom."'";
        $this->setQuery($delete_query);
        return $this->executeQuery();
    }
    public function danhSachNhom(){
	$query = "SELECT ma_nhom, ten_nhom, ngay_thanh_lap, thong_tin, luot_like FROM nhom_nhac";
	//$result = mysql_query($query, $connection) or die("SQL Error 1: " . mysql_error());
	$this->setQuery($query);
        return $this->executeQuery();
    }
    public function danhSachThanhVien(){
        $query = "SELECT a.ma_nghe_si, a.ngay_gia_nhap, a.ngay_roi_nhom, b.nghe_danh
                  FROM thanh_vien_nhom a JOIN nghe_si b ON a.ma_nghe_si = b.ma_nghe_si
                  AND a.ngay_roi_nhom IS NULL AND a.ma_nhom=".$this->maNhom;
        $this->setQuery($query);
        return $this->executeQuery();
    }
    public function danhSachNghesiChuaCoNhom(){
        $query = "SELECT ma_nghe_si, nghe_danh FROM nghe_si WHERE con_hoat_dong = true AND ma_nghe_si not in
                 (SELECT ma_nghe_si FROM thanh_vien_nhom WHERE ngay_roi_nhom IS NULL)";
        $this->setQuery($query);
        return $this->executeQuery();
    }
    public function themThanhVien($maThanhVien, $ngayGiaNhap){
        $insert_query = "INSERT INTO thanh_vien_nhom VALUES ('".$maThanhVien."','".$this->maNhom."','".$ngayGiaNhap."', NULL)";
        $this->setQuery($insert_query);
        return $this->executeQuery();
    }
    public function choRoiNhom($maThanhVien, $ngayRoiNhom){
        $update_query = "UPDATE thanh_vien_nhom SET ngay_roi_nhom='".$ngayRoiNhom."' WHERE ma_nghe_si='".$maThanhVien."'";
        $this->setQuery($update_query);
	return $this->executeQuery();
    }
}
?>